﻿using UnityEngine;
using System.Collections;

public class UIFollowObj : MonoBehaviour {
	public GameObject TargetObject;
	float smooth = 50f;
	// Use this for initialization
	void Start ()
	{
		//worldcamera = NGUITools.FindCameraForLayer(TargetObject.layer);     //这里是通过物体的层获得相应层上的相机
		//guiCamera = NGUITools.FindCameraForLayer(this.gameObject.layer);   //通过脚本所在物体的层获得相应层上的相机
		//获取目标物体的屏幕坐标
		Vector3 pos = Camera.main.WorldToScreenPoint(TargetObject.transform.position + new Vector3(0,2,0));
		pos.z = 0;                                //由于NGUI 2D界面的Z轴都为0，这里我们将坐标修改为0.只取其X,Y坐标。
		transform.position = Vector3.Lerp(transform.position, pos, smooth*Time.deltaTime);

	}

	//void LateUpdate()
	void LateUpdate()
	{	
		if (TargetObject == null) {
			Destroy (gameObject);
		}
		//获取目标物体的屏幕坐标
		Vector3 pos = Camera.main.WorldToScreenPoint(TargetObject.transform.position + new Vector3(0,2,0));
		pos.z = 0;                                //由于NGUI 2D界面的Z轴都为0，这里我们将坐标修改为0.只取其X,Y坐标。
		transform.position = Vector3.Lerp(transform.position, pos, smooth*Time.deltaTime);
	}
}
